<?php
/*
Plugin Name: Gửi liên hệ
Plugin URI: http://wp.tutsplus.com/author/barisunver/
Description: Tạo một form liên hệ. Sử dụng: <code>[contact email="your@email.address"]</code>
Version: 1.0
Author: Phan Van Nhien
Author URI: 
*/

// function to get the IP address of the user
function wptuts_get_the_ip() {
	if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		return $_SERVER["HTTP_X_FORWARDED_FOR"];
	}
	elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
		return $_SERVER["HTTP_CLIENT_IP"];
	}
	else {
		return $_SERVER["REMOTE_ADDR"];
	}
}

// the shortcode
function wptuts_contact_form_sc($atts) {
	extract(shortcode_atts(array(
		"email" => get_bloginfo('admin_email'),
		"subject" => '',
		"label_name" => 'Tên của bạn (*)',
		"label_email" => 'Email của bạn (*)',
		"label_subject" => 'Đề tài (*)',
		"label_message" => 'Nội dung gửi (*)',
		"label_submit" => 'Gửi',
		"error_empty" => 'Vui lòng điền đầy đủ thông tin yêu cầu.',
		"error_noemail" => 'Vui lòng gõ đúng địa chỉ email.',
		"success" => 'Cảm ơn bạn đã liên hệ! Chúng tôi sẽ phản hồi bạn sớm nhất.'
	), $atts));

	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		$error = false;
		$required_fields = array("your_name", "email", "message", "subject");

		foreach ($_POST as $field => $value) {
			if (get_magic_quotes_gpc()) {
				$value = stripslashes($value);
			}
			$form_data[$field] = strip_tags($value);
		}

		foreach ($required_fields as $required_field) {
			$value = trim($form_data[$required_field]);
			if(empty($value)) {
				$error = true;
				$result = $error_empty;
			}
		}

		if(!is_email($form_data['email'])) {
			$error = true;
			$result = $error_noemail;
		}

		if ($error == false) {
			$email_subject = "[" . get_bloginfo('name') . "] " . $form_data['subject'];
			$email_message = $form_data['message'] . "\n\nIP: " . wptuts_get_the_ip();
			$headers  = "From: ".$form_data['your_name']." <".$form_data['email'].">\n";
			$headers .= "Content-Type: text/plain; charset=UTF-8\n";
			$headers .= "Content-Transfer-Encoding: 8bit\n";
			wp_mail($email, $email_subject, $email_message, $headers);
			$result = $success;
			$sent = true;
		}
	}

	
	$email_form = '<form class="contact-form" method="post" action="'.get_permalink().'">
		<div class="rows">
			<label for="cf_name">'.$label_name.':</label>
			<input type="text" name="your_name" id="cf_name" size="50" maxlength="50" value="'.$form_data['your_name'].'" />
		</div>
		<div  class="rows">
			<label for="cf_email">'.$label_email.':</label>
			<input type="text" name="email" id="cf_email" size="50" maxlength="50" value="'.$form_data['email'].'" />
		</div>
		<div  class="rows">
			<label for="cf_subject">'.$label_subject.':</label>
			<input type="text" name="subject" id="cf_subject" size="50" maxlength="50" value="'.$subject.$form_data['subject'].'" />
		</div>
		<div  class="rows" >
			<label for="cf_message">'.$label_message.':</label>
			<textarea name="message" id="cf_message" cols="60" rows="10">'.$form_data['message'].'</textarea>
		</div>
		<div class="rows">
			<input class="button" type="submit" value="'.$label_submit.'" name="send" id="cf_send" />
		</div>
	</form>';
	
	if($result != "") {
		$info = '<div class="info">'.$result.'</div>';
	}
	if($sent == true) {
		return $info;
	} else {
		return $info.$email_form;
	}
} add_shortcode('contact', 'wptuts_contact_form_sc');

?>